package org.kyw.system.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.kyw.basic.util.AjaxResult;
import org.kyw.basic.util.PageList;
import org.kyw.system.domain.SystemDictionaryDetail;
import org.kyw.system.query.SystemDictionaryDetailQuery;
import org.kyw.system.service.ISystemDictionaryDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 数据字典明细管理接口
 */

@RestController
@RequestMapping("/systemDictionaryDetail")
@Api(tags = "数据字典明细接口",description = "数据字典明细接口详细描述")
public class SystemDictionaryDetailController {

    @Autowired
    private ISystemDictionaryDetailService systemDictionaryDetailService;

    /**
     * 修改或新增数据字典明细
     */
    @PutMapping
    @ApiOperation(value = "数据字典明细添加或修改",notes = "如果有id就是修改否则就是添加")
    public AjaxResult addOrUpdate(@RequestBody SystemDictionaryDetail systemDictionaryDetail){
        try {
            if(systemDictionaryDetail.getId() == null){
                systemDictionaryDetailService.add(systemDictionaryDetail);
            }else {
                systemDictionaryDetailService.update(systemDictionaryDetail);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult
                    .me()
                    .setSuccess(false)
                    .setMessage("系统繁忙，请稍后重试");
        }
    }

    /**
     * 删除一条记录
     */
    @DeleteMapping("/{id}")
    public AjaxResult removeById(@PathVariable("id") Long id){
        try {
            systemDictionaryDetailService.remove(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult
                    .me()
                    .setSuccess(false)
                    .setMessage("系统繁忙，请稍后重试");
        }
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @PatchMapping
    public AjaxResult batchRemove(@RequestBody List<Long> ids){
        try {
            systemDictionaryDetailService.batchRemove(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult
                    .me()
                    .setSuccess(false)
                    .setMessage("系统繁忙，请稍后重试");
        }
    }

    /**
     * 查询一条记录
     * @param id
     * @return
     */
    //@RequestMapping(value = "/1", method = RequestMethod.GET)
    @GetMapping("/{id}")
    public SystemDictionaryDetail queryById(@PathVariable("id") Long id){
        return systemDictionaryDetailService.queryById(id);
    }

    /**
     * 查询所有
     * @return
     */
    @GetMapping
    public List<SystemDictionaryDetail> queryAll(){
        return systemDictionaryDetailService.queryAll();
    }

    /**
     * 分页 + 高级查询
     * @return
     */
    @PostMapping("/list")
    public PageList<SystemDictionaryDetail> queryPage(@RequestBody Map<String, String> param){
        SystemDictionaryDetailQuery query = new SystemDictionaryDetailQuery();
        query.setCurrentPage(Integer.valueOf(param.get("currentPageDetail")));
        query.setPageSize(Integer.valueOf(param.get("pageSizeDetail")));
        query.setKeyword(param.get("keyword"));
        query.setTypes_id(Long.valueOf(param.get("types_id")));
        return systemDictionaryDetailService.queryPage(query);
    }
}
